package cn.jsu.oj.dispatchServer.satellite.handler;

import cn.jsu.oj.dispatchServer.satellite.annotation.HandlerType;
import cn.jsu.oj.dispatchServer.satellite.factory.Factory;
import cn.jsu.oj.dispatchServer.satellite.pojo.net.PackNet;
import cn.jsu.oj.dispatchServer.satellite.pojo.net.RequestNet;
import cn.jsu.oj.dispatchServer.satellite.pojo.net.ResponseNet;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.Arrays;

/**
 * 服务器重启处理器
 *
 * @author shan
 * @date 2022/07/11
 */
@Slf4j
@Component("rebootHandler")
@HandlerType(typeNo = 13)
public class RebootHandler implements Handler {
    
    @Autowired
    Factory handlerFactory;
    
    @Override
    public void handle(PackNet<?> packNet) throws FileNotFoundException {
        String uuid = ((RequestNet) packNet.getMessage()).getUuid();
        ((Handler) handlerFactory.getProduct((short) 4))
                .handle(new PackNet<>(packNet.getChannel()
                        , new ResponseNet(uuid, true, (short) 13)));
        try {
            log.warn("----------指令收到，即将重启......");
            Runtime.getRuntime().exec("reboot");
        } catch (IOException e) {
            e.printStackTrace();
            log.error("----------重启失败......");
            log.error(Arrays.toString(e.getStackTrace()));
        }
    }
}
